﻿using Com.JunCaiSoft.API.Common.AssistClass;
using Com.JunCaiSoft.Common.Dao;
using Com.JunCaiSoft.SystemConfig.Entity;
using Com.JunCaiSoft.SystemConfig.IDao;
using Com.JunCaiSoft.WXWork.Common;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.SystemConfig.Dao
{
    public class AppSecretManageDao : CoreDALAsync<AppSecretManage>, IAppSecretManageDao
    {
        public AppSecretManageDao(DbContext context) : base(context)
        {

        }

        public DbContext dbContext { get { return baseContext; } }

        public async Task<Int32> AgentIDAsync(string AppName)
        {
            String agentid = RedisCacheExtension.GetStringValue(AppName + "AgentID");

            if (String.IsNullOrEmpty(agentid))
            {
                var agentidItem = await GetAsync(x => x.AppName == AppName);

                RedisCacheExtension.SetStringValue(AppName + "AgentID", agentidItem.AgentID);

                agentid = RedisCacheExtension.GetStringValue(AppName + "AgentID");
            }

            return Int32.Parse(agentid);
        }

        /// <summary>
        /// 获取AppKey
        /// </summary>
        /// <param name="AppName"></param>
        /// <returns></returns>
        public async Task<string> AppKeyAsync(string AppName)
        {
            String appkey = RedisCacheExtension.GetStringValue(AppName + "appkey");

            if (String.IsNullOrEmpty(appkey))
            {
                var appkeyItem = await GetAsync(x => x.AppName == AppName);

                RedisCacheExtension.SetStringValue(AppName + "AppKey", appkeyItem.AppKey);

                appkey = RedisCacheExtension.GetStringValue(AppName + "AppKey");
            }

            return appkey;
        }
        /// <summary>
        /// 获取AppSecret
        /// </summary>
        /// <param name="AppName"></param>
        /// <returns></returns>
        public async Task<string> AppSecretManagerAsync(string AppName)
        {
            String corpsecret = RedisCacheExtension.GetStringValue(AppName + "AppSecret");

            if (String.IsNullOrEmpty(corpsecret))
            {
                var appSecretItem = await GetAsync(x => x.AppName == AppName);

                RedisCacheExtension.SetStringValue(AppName + "AppSecret", appSecretItem.AppSecret);

                corpsecret = RedisCacheExtension.GetStringValue(AppName + "AppSecret");
            }

            return corpsecret;
        }
    }
}
